<form>
    <label>请输入域名：</label>
    <input type="text" name="url" value="<?php echo $_REQUEST['url'] ?? '';?>">
    <input type="submit">
</form>
<?php
/**
 * 域名可包含字母、数字、点号、连字符
 * 域名校验
 * 分析出域名具体品相。
 * 111.com 三位数字
 * jmkj.com 四位纯声母
 * jmkjabc.com 7位字母
 * xian.com 单拼
 * xianguang.com 两拼
 * a1a2.com 杂米
 * abc.com.cn  3位字母
 */

$url = $_REQUEST['url'] ?? '' ;
//$url = 'baidu.com';
//$url = 'xianguang.com';
//$url = 'jmkj.com';
//$url = 'abc.com.cn';
//$url = '111.com';
//$url = 'xian.com';
//$url = 'a1a2.com';
if ('' == trim($url)) {
    exit();
}


//验证域名
if(!filter_var($url, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
//if($url = filter_var($url, FILTER_VALIDATE_URL)) {
    echo '域名格式错误' . "\n";
}
if (false === strpos($url,'.')) {
    echo '域名格式错误:应该包含.' . "\n";
}

$name = strstr($url, '.', true);

switch (true) {
    case preg_match('/^\d+$/', $name):
        echo strlen($name) . '位数字' . "\n";
        break;
    case isZaMi($name):
        echo '杂米' . "\n";
        break;
    case preg_match('/^(b|p|m|f|d|t|n|l|g|k|h|j|q|x|zh|ch|sh|r|z|c|s|y|w)+$/', $name):
        echo strlen($name) . '位纯声母' . "\n";
        break;
    case $pinyinArr = isPinyin($name):
        echo count($pinyinArr) . '拼' . "\n";
        break;
    case preg_match('/^\w+$/', $name):
        echo strlen($name) . '位字母' . "\n";
        break;
    default :
        echo '其他';
}


function isPinyin($string)
{
    // 定义拼音音节数组
    $pinyinSyllables = [
        "ba","bai","ban","bang","bao","bei","ben","beng","bi","bian","biao","bie","bin","bing","bo","bu",
        "ca","cai","can","cang","cao","ce","cen","ceng","ci","cong","cou","cu","cuan","cui","cun","cuo",
        "da","dai","dan","dang","dao","de","dei","den","deng","di","dia","dian","diao","die","ding","diu","dong","dou","du","duan","dui","dun","duo",
        "fa","fan","fang","fei","fen","feng","fo","fou","fu",
        "ga","gai","gan","gang","gao","ge","gei","gen","geng","gong","gou","gu","gua","guai","guan","guang","gui","gun","guo",
        "ha","hai","han","hang","hao","he","hei","hen","heng","hong","hou","hu","hua","huai","huan","huang","hui","hun","huo",
        "ji","jia","jian","jiang","jiao","jie","jin","jing","jiong","jiu","ju","juan","jue","jun",
        "ka","kai","kan","kang","kao","ke","ken","keng","kong","kou","ku","kua","kuai","kuan","kuang","kui","kun","kuo",
        "la","lai","lan","lang","lao","le","lei","leng","li","lia","lian","liang","liao","lie","lin","ling","liu","long","lou","lu","lv","luan","lue","lun","luo",
        "ma","mai","man","mang","mao","me","mei","men","meng","mi","mian","miao","mie","min","ming","miu","mo","mou","mu",
        "na","nai","nan","nang","nao","ne","nei","nen","neng","ni","nian","niang","niao","nie","nin","ning","niu","nong","nou","nu","nv","nuan","nue","nuo",
        "pa","pai","pan","pang","pao","pei","pen","peng","pi","pian","piao","pie","pin","ping","po","pu",
        "qi","qia","qian","qiang","qiao","qie","qin","qing","qiong","qiu","qu","quan","que","qun",
        "ran","rang","rao","re","ren","reng","ri","rong","rou","ru","ruan","rui","run","ruo",
        "sa","sai","san","sang","sao","se","sen","seng","si","song","sou","su","suan","sui","sun","suo",
        "ta","tai","tan","tang","tao","te","teng","ti","tian","tiao","tie","ting","tong","tou","tu","tuan","tui","tun","tuo",
        "wa","wai","wan","wang","wei","wen","weng","wo","wu",
        "xi","xia","xian","xiang","xiao","xie","xin","xing","xiong","xiu","xu","xuan","xue","xun",
        "ya","yan","yang","yao","ye","yi","yin","ying","yo","yong","you","yu","yuan","yue","yun",
        "za","zai","zan","zang","zao","ze","zei","zen","zeng","zi","zong","zou","zu","zuan","zui","zun","zuo",
        "zha","zhai","zhan","zhang","zhao","zhe","zhen","zheng","zhi","zhong","zhou","zhu","zhua","zhuai","zhuan","zhuang","zhui","zhun","zhuo",
        "cha","chai","chan","chang","chao","che","chen","cheng","chi","chong","chou","chu","chua","chuai","chuan","chuang","chui","chun","chuo",
        "sha","shai","shan","shang","shao","she","shen","sheng","shi","shou","shu","shua","shuai","shuan","shuang","shui","shun","shuo",
        "ra","re","ren","reng","ri","rong","rou","ru","ruan","rui","run","ruo",
        "ai","an","ang","ao",
        "ou",
        "ei","en","eng","er",
        "a","o","e"
    ];
    usort($pinyinSyllables, function ($a, $b){return strlen($a) < strlen($b);});
    $pattern = '/^(' . implode('|',$pinyinSyllables) . ')+$/';
    // 如果字符串与正则表达式匹配，则返回 true，否则返回 falses
    $pinyinArr = [];

    $res  = preg_match($pattern, $string, $matches, PREG_OFFSET_CAPTURE);
    //var_dump($string, $res, $matches);

    if ($res === 1) {
        $pinyinArr[] = $matches[1][0];
        $string = substr($string, 0, $matches[1][1]);
        $pinyinArr = array_merge(isPinyin($string),$pinyinArr);
    }
    return $pinyinArr;
}

function isZaMi($name)
{
    $isNum = preg_match('/\d/', $name);
    $isAlpha = preg_match('/\w/', $name);
    $isHyphen = preg_match('/-/', $name);
    $count = $isNum+$isAlpha+$isHyphen;
    return $count>1 ? true : false;
}



